package cn.springcloud.api.apiUtils;


import org.apache.commons.lang3.time.FastDateFormat;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PathFormat {

    private static final String TIME = "time";
    private static final String FULL_YEAR = "yyyy";
    private static final String YEAR = "yy";
    private static final String MONTH = "MM";
    private static final String DAY = "dd";
    private static final String HOUR = "hh";
    private static final String HOUR_UPPER = "HH";
    private static final String MINUTE = "ii";
    private static final String SECOND = "ss";
    private static final String RAND = "rand";

    private static ThreadLocal<Date> currentDate = new ThreadLocal();

    private static Pattern PATTERN = Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);

    public static String parse(String input) {
        Matcher matcher = PATTERN.matcher(input);
        currentDate.set(new Date());

        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {

            matcher.appendReplacement(sb, getString(matcher.group(1)));

        }

        matcher.appendTail(sb);

        return sb.toString();
    }



    public static String parse(String input, String filename) {

        Matcher matcher = PATTERN.matcher(input);
        String matchStr;
        currentDate.set(new Date());

        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {

            matchStr = matcher.group(1);
            if (matchStr.contains("filename")) {
                filename = filename.replace("$", "\\$").replaceAll("[\\/:*?\"<>|]", "");
                matcher.appendReplacement(sb, filename);
            } else {
                matcher.appendReplacement(sb, getString(matchStr));
            }

        }

        matcher.appendTail(sb);

        return sb.toString();
    }

    private static String getString(String pattern) {
        // time 处理
        if (pattern.contains(TIME)) {
            return getTimestamp();
        } else if (pattern.contains(FULL_YEAR)) {
            return getFullYear();
        } else if (pattern.contains(YEAR)) {
            return getYear();
        } else if (pattern.contains(MONTH)) {
            return getMonth();
        } else if (pattern.contains(DAY)) {
            return getDay();
        } else if (pattern.contains(HOUR)) {
            return getHour();
        } else if (pattern.contains(MINUTE)) {
            return getMinute();
        } else if (pattern.contains(SECOND)) {
            return getSecond();
        } else if (pattern.contains(RAND)) {
            return getRandom(pattern);
        }

        return pattern;

    }

    private static String getTimestamp() {
        return System.currentTimeMillis() + "";
    }

    private static String getFullYear() {
        return FastDateFormat.getInstance(FULL_YEAR).format(currentDate.get());
    }

    private static String getYear() {
        return FastDateFormat.getInstance(YEAR).format(currentDate.get());
    }

    private static String getMonth() {
        return FastDateFormat.getInstance(MONTH).format(currentDate.get());
    }

    private static String getDay() {
        return FastDateFormat.getInstance(DAY).format(currentDate.get());
    }

    private static String getHour() {
        return FastDateFormat.getInstance(HOUR_UPPER).format(currentDate.get());
    }

    private static String getMinute() {
        return FastDateFormat.getInstance(MINUTE).format(currentDate.get());
    }

    private static String getSecond() {
        return FastDateFormat.getInstance(SECOND).format(currentDate.get());
    }

    private static String getRandom(String pattern) {

        int length = 0;
        pattern = pattern.split(":")[1].trim();

        length = Integer.parseInt(pattern);

        return (Math.random() + "").replace(".", "").substring(0, length);

    }

}
